package com.baiqian.doupo.entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.IPacket;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nullable;
import java.util.List;
import java.util.UUID;

import static net.minecraft.util.EntityPredicates.IS_ALIVE;

public class DouJiShiShanLieEntity extends Entity {
    private static final DataParameter<Integer> COUNTER = EntityDataManager.createKey(DouJiShiShanLieEntity.class, DataSerializers.VARINT);
    private UUID owner;
    private int field_234610_c_;
    private float damage = 0.0f;

    DouJiShiShanLieEntity(EntityType entityTypeIn, World worldIn) {
        super(entityTypeIn, worldIn);
    }

    public void setDamage(float damage){
        this.damage = damage;
    }

    public void setShooter(@Nullable Entity p_212361_1_) {
        if (p_212361_1_ != null) {
            this.owner = p_212361_1_.getUniqueID();
            this.field_234610_c_ = p_212361_1_.getEntityId();
        }
    }
    @Nullable
    public Entity getShooter() {
        if (this.owner != null && this.world instanceof ServerWorld) {
            return ((ServerWorld)this.world).getEntityByUuid(this.owner);
        } else {
            return this.field_234610_c_ != 0 ? this.world.getEntityByID(this.field_234610_c_) : null;
        }
    }

    @Override
    protected void registerData() {
        this.dataManager.register(COUNTER, 0);
    }

    @Override
    protected void readAdditional(CompoundNBT compound) {
        this.dataManager.set(COUNTER, compound.getInt("counter"));
    }

    @Override
    protected void writeAdditional(CompoundNBT compound) {
        compound.putInt("counter", this.dataManager.get(COUNTER));
    }

    @Override
    public void tick() {
        if (world.isRemote) {
//            logger.info(this.dataManager.get(COUNTER));
        }
        if (!world.isRemote) {
//            logger.info(this.dataManager.get(COUNTER));
            this.dataManager.set(COUNTER, this.dataManager.get(COUNTER) + 1);
            this.setPosition(this.getPosX()+0.1*Math.sin(-Math.toRadians(this.rotationYaw)),
                    this.getPosY()+0.1*Math.sin(-Math.toRadians(this.rotationPitch)),
                    this.getPosZ()+0.1*Math.cos(-Math.toRadians(this.rotationYaw)));

            Entity shooter = this.getShooter();
            if (shooter != null) {
                AxisAlignedBB range = new AxisAlignedBB(
                        this.getPosX()- 0, this.getPosY() - 0,  this.getPosZ()-0,
                        this.getPosX() +1, this.getPosY() + 1,  this.getPosZ()+1);//表示x，y，z所在的这一个格子
                List<LivingEntity> livingEntities = this.world.getEntitiesWithinAABB(LivingEntity.class,range,IS_ALIVE);
                for (int i = 0; i < livingEntities.size(); ++i) {
                    LivingEntity livingEntity = livingEntities.get(i);
                    //对他人增加效果
//                    if(livingEntity.getPosX()!=entity.getPosX()||livingEntity.getPosY()!=entity.getPosY()||livingEntity.getPosZ()!=entity.getPosZ()) {
//                        EffectInstance effectinstance = new EffectInstance(Effects.GLOWING, 5, 0, false, false, false);
//                        livingEntity.addPotionEffect(effectinstance);
//                    }
                    DamageSource damagesource;
                    if (shooter == null) {
                        damagesource = DamageSource.causeThornsDamage(this);
                    } else {
                        damagesource = DamageSource.causeThornsDamage(shooter);
                        if (shooter instanceof LivingEntity) {
                            ((LivingEntity)shooter).setLastAttackedEntity(livingEntity);
                        }
                    }
                    livingEntity.attackEntityFrom(damagesource, damage);
                }
            }
            if(this.dataManager.get(COUNTER)>100)
                this.setDead();
        }
        super.tick();
    }

    @Override
    public IPacket<?> createSpawnPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }
}
